package com.eshealthy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eshealthy.entity.Drug;
import com.eshealthy.entity.Library;
import com.eshealthy.entity.MedicalDevice;
import org.apache.http.HttpHost;
import org.apache.tomcat.jni.LibraryNotFoundError;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.*;

import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.client.indices.DetailAnalyzeResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FullSearch {
    public long count(Integer flag,String key)throws IOException {
        String name="";
        String type="";
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        if(flag==1){name="drug";type="useage";}
        else if(flag==2){
             name="medical_device";
             type="directions";
        }
        else {
             name="library";
             type="directions";
        }
        SearchRequest searchRequest=new SearchRequest(name);
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery(type,key));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse=esClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("符合条件的总数"+searchResponse.getHits().getTotalHits().value);
        System.out.println("最大得分是"+searchResponse.getHits().getMaxScore());
      return searchResponse.getHits().getTotalHits().value;
    }
    public List<MedicalDevice>  Devices(Integer page, Integer rows,String key) throws IOException {
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //3，构建QueryBuilder对象指定查询方式和查询条件

//      //4,将QuseryBuilder对象设置到SearchSourceBuilder对象中

        //1，构建SearchRequest请求对象，指定索引库
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("medical_device");
        //2,构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("directions",key));

        //分页
        if(page!=0){page=(page-1)*4;}
        System.out.println(page);
        searchSourceBuilder.from(page);
        searchSourceBuilder.size(rows);

        //5，将SearchSourceBuilder设置到SearchRequest中
        searchRequest.source(searchSourceBuilder);
        // 3.查询
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);
//      System.out.println(searchResponse.toString().replace(" ",""));
        List<MedicalDevice> medicalDeviceList=new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(searchResponse.toString().replace(" ",""));
        JSONObject json = JSON.parseObject(jsonObject.getString("hits").toString());
//        System.out.println(json);
        JSONArray jsonArray= JSON.parseArray(json.getString("hits"));
        for (int i=0;i<jsonArray.size();i++){
//        System.out.println(jsonArray.getJSONObject(i).get("_source"));
            MedicalDevice medicalDevice=JSONObject.parseObject(jsonArray.getJSONObject(i).get("_source").toString(),MedicalDevice.class);
            medicalDeviceList.add(medicalDevice);
            System.out.println(medicalDevice);
        }

        return medicalDeviceList;
    }
    public List<Drug>  Drugs(Integer page, Integer rows, String key) throws IOException {
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //3，构建QueryBuilder对象指定查询方式和查询条件

//      //4,将QuseryBuilder对象设置到SearchSourceBuilder对象中

        //1，构建SearchRequest请求对象，指定索引库
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("drug");
        //2,构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("useage",key));

        //分页
        if(page!=0){page=(page-1)*4;}
        System.out.println(page);
        searchSourceBuilder.from(page);
        searchSourceBuilder.size(rows);

        //5，将SearchSourceBuilder设置到SearchRequest中
        searchRequest.source(searchSourceBuilder);
        // 3.查询
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);
//      System.out.println(searchResponse.toString().replace(" ",""));
        List<Drug> DrugList=new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(searchResponse.toString().replace(" ",""));
        JSONObject json = JSON.parseObject(jsonObject.getString("hits").toString());
//        System.out.println(json);
        JSONArray jsonArray= JSON.parseArray(json.getString("hits"));
        for (int i=0;i<jsonArray.size();i++){
//        System.out.println(jsonArray.getJSONObject(i).get("_source"));
            Drug drugs=JSONObject.parseObject(jsonArray.getJSONObject(i).get("_source").toString(),Drug.class);
            DrugList.add(drugs);
        }

        return DrugList;
    }
    public List<Library>  Librarys(Integer page, Integer rows, String word) throws IOException {
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //3，构建QueryBuilder对象指定查询方式和查询条件

//      //4,将QuseryBuilder对象设置到SearchSourceBuilder对象中

        //1，构建SearchRequest请求对象，指定索引库
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("library");
        //2,构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("directions",word));

        //分页
        if(page!=0){page=(page-1)*4;}
        System.out.println(page);
        searchSourceBuilder.from(page);
        searchSourceBuilder.size(rows);

        //5，将SearchSourceBuilder设置到SearchRequest中
        searchRequest.source(searchSourceBuilder);
        // 3.查询
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);
//      System.out.println(searchResponse.toString().replace(" ",""));
        List<Library> DrugList=new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(searchResponse.toString().replace(" ",""));
        JSONObject json = JSON.parseObject(jsonObject.getString("hits").toString());
//        System.out.println(json);
        JSONArray jsonArray= JSON.parseArray(json.getString("hits"));
        for (int i=0;i<jsonArray.size();i++){
//        System.out.println(jsonArray.getJSONObject(i).get("_source"));
            Library libraries=JSONObject.parseObject(jsonArray.getJSONObject(i).get("_source").toString(),Library.class);
            DrugList.add(libraries);
        }

        return DrugList;
    }
    public long count(Integer key)throws IOException{
        String name="";
        if (key==1){
            name="drug";
        }else if(key==2){
            name="medical_device";
        }else{
            name="library";
        }
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //查询数据
        GetRequest request =new GetRequest();
        //查询的索引和id
        CountRequest countRequest=new CountRequest(name);
        CountResponse count = esClient.count(countRequest, RequestOptions.DEFAULT);
        return count.getCount();
    }

}
